package com.alan.swagger2.controller;

import com.alan.swagger2.bean.*;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: Alan liu
 * @Date: 2018/10/31 10:31
 * @Description:
 */
@Controller
@RequestMapping("dwz")
public class DwzController {

    @RequestMapping(value = "login", method = RequestMethod.GET)
    private String login(){
        return "/html/dwz/login";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    private String login(String userName, String password, Model model){
        String msg = "";
        if(StringUtils.isAnyEmpty(userName,password)){
            msg = "用户名或密码不能为空";
        }else {
            if(userName.equalsIgnoreCase("admin")&&
                    password.equalsIgnoreCase("123456")){
                return "redirect:"+"/dwz/main";
            }else {
                msg = "用户名名密码错误";
            }
        }
        model.addAttribute("msg",msg);
        return "/html/dwz/login";
    }

    @RequestMapping(value = "main", method = RequestMethod.GET)
    private String main(){
        return "/html/dwz/main";
    }

    @RequestMapping(value = "userList", method = {RequestMethod.GET,RequestMethod.POST})
    private String userList(Model model,Integer pageNum){
        System.out.println("pageNum : "+pageNum);
        Integer numPerPage;
        if( pageNum == null){
            numPerPage = 1;
        }else {
            numPerPage = pageNum;
        }
        List<User> users = new ArrayList<>();
        User user = new User();
        user.setAge(11);
        user.setId(1);
        user.setUsername("test");
        User user2 = new User();
        user2.setAge(112);
        user2.setId(12);
        user2.setUsername("test2");
        User user3 = new User();
        user3.setAge(113);
        user3.setId(13);
        user3.setUsername("test3");
        users.add(user);
        users.add(user2);
        users.add(user3);
        model.addAttribute("userList",users);
        model.addAttribute("totalCount",users.size());
        model.addAttribute("numPerPage",numPerPage);
        return "/html/dwz/page/userList";
    }

    @RequestMapping(value = "user/delete", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    private ResultBean<String> delete(Model model, Integer uid){
        System.out.println("uid : "+uid);
        ResultBean resultBean = new ResultBean<String>();
        resultBean.setResult("OK");
        return resultBean;
    }

    @RequestMapping(value = "user/add", method =RequestMethod.GET)
    private String add(Model model, Integer uid){
        System.out.println("uid : "+uid);
        return "/html/dwz/page/userAdd";
    }

    @RequestMapping(value = "user/save", method ={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    private DwzRes save(User user,Model model){
        System.out.println("user : "+ JSON.toJSONString(user));
        DwzRes dwzRes = new DwzRes();
        return dwzRes;
    }



}
